﻿using HR.IService;
using HR.Models;

namespace HR.Service
{
    public class SalaryStandardDetailService : ISalaryStandardDetailService
    {
        private readonly HRDBContext db;

        public SalaryStandardDetailService(HRDBContext db)
        {
            this.db = db;
        }

        public Task<int> SalaryStandardDetailAdd(List<salary_standard_detail> list)
        {
            return Task.Run(() =>
            {
                foreach (salary_standard_detail item in list)
                {
                    db.salary_standard_details.Add(item);
                }
                return db.SaveChanges();
            });

        }

        public Task<List<salary_standard_detail>> SalaryStandardDetailBySid(string standard_id)
        {
            return Task.Run(() =>
            {
                List<salary_standard_detail> list = db.salary_standard_details.Where(ssd => ssd.standard_id == standard_id).ToList();
                return list;
            });
        }

        public Task<int> SalaryStandardDetailUdp(List<salary_standard_detail> list)
        {
            return Task.Run(() =>
            {
                foreach (salary_standard_detail item in list)
                {
                    salary_standard_detail? ssd = db.salary_standard_details.Find(item.sdt_id);
                    ssd.salary = item.salary;
                }
                return db.SaveChanges();
            });
        }

        public Task<List<salary_standard_detail>> SalaryStandardDetailsFindHmid(string human_id)
        {
            return Task.Run(() =>
            {
                //先利用档案ID查询到对应的档案信息
                human_file? hf = db.human_files.Where(hf => hf.human_id == human_id).FirstOrDefault();
                //再根据档案信息中的薪酬标准单编号查询到薪酬标准明细
                List<salary_standard_detail> list = db.salary_standard_details.Where(ssd => ssd.standard_id == hf.salary_standard_id).ToList();
                return list;
            });
         }
    }
}
